using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using NTTHAO.Web.BL.CommonPage;
using NTTHAO.Web.BL.Product;

namespace NTTHAO.Sites.CommonSite
{
    /// <summary>
    /// Summary description for RewriteModule
    /// </summary>
    public static class RewriteModule
    {
        public static void RewriteUrl(HttpRequest request, HttpContext context)
        {
            if (!request.PhysicalPath.Contains("\"") && File.Exists(request.PhysicalPath))
            {
                return;
            }
            string virtualPath = request.Path.Trim().ToLower();
            string pathAndQuery = request.Url.PathAndQuery.Trim().ToLower();
            string realURL = "";

            if (virtualPath.StartsWith("/admin/", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }
            else if (virtualPath.StartsWith("/resources/", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }
            else if (virtualPath.StartsWith("/lang/", StringComparison.OrdinalIgnoreCase))
            {
                string lang = virtualPath.Replace("/lang/", "").Replace("/", "");
                if ("en".Equals(lang, StringComparison.OrdinalIgnoreCase))
                {
                    realURL = "~/SetLanguage.aspx?lang=2";
                }
                else
                {
                    realURL = "~/SetLanguage.aspx?lang=1";
                }
            }
            else if (virtualPath.StartsWith("/WebResource.axd", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }
            else if (virtualPath.Contains("/"))
            {
                string[] paths = virtualPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                if (paths.Length > 0)
                {
                    string gText = paths[0].Trim();
                    CommonPageGroup group = CommonPageGroup.GetPageGroup(gText);
                    if (group.ID < 1)
                    {
                        return;
                    }
                    if (paths.Length > 1 && "search".Equals(paths[1], StringComparison.OrdinalIgnoreCase))
                    {
                        realURL += "~/default.aspx?type=search&g=" + group.ID.ToString();
                        if (request.ServerVariables["QUERY_STRING"] != null)
                        {
                            realURL += "&" + request.ServerVariables["QUERY_STRING"];
                        }
                    }
                    else if (paths.Length > 1 && "booking".Equals(paths[1], StringComparison.OrdinalIgnoreCase))
                    {
                        realURL += "~/default.aspx?type=order&g=" + group.ID.ToString();
                        if (request.ServerVariables["QUERY_STRING"] != null)
                        {
                            realURL += "&" + request.ServerVariables["QUERY_STRING"];
                        }
                    }
                    else if (paths.Length > 1 && "gio-hang".Equals(paths[1], StringComparison.OrdinalIgnoreCase))
                    {
                        realURL += "~/default.aspx?type=cart&g=" + group.ID.ToString();
                        if (request.ServerVariables["QUERY_STRING"] != null)
                        {
                            realURL += "&" + request.ServerVariables["QUERY_STRING"];
                        }
                    }
                    else if (paths.Length > 1 && "mua-hang".Equals(paths[1], StringComparison.OrdinalIgnoreCase))
                    {
                        realURL += "~/default.aspx?type=buy&g=" + group.ID.ToString();
                        if (request.ServerVariables["QUERY_STRING"] != null)
                        {
                            realURL += "&" + request.ServerVariables["QUERY_STRING"];
                        }
                    }
                    else if (paths.Length > 1 && "thanh-toan".Equals(paths[1], StringComparison.OrdinalIgnoreCase))
                    {
                        realURL += "~/default.aspx?type=pay&g=" + group.ID.ToString();
                        if (request.ServerVariables["QUERY_STRING"] != null)
                        {
                            realURL += "&" + request.ServerVariables["QUERY_STRING"];
                        }
                    }
                    else if (paths.Length > 1 && "ket-thuc-mua-hang".Equals(paths[1], StringComparison.OrdinalIgnoreCase))
                    {
                        realURL += "~/default.aspx?type=buyfinished&g=" + group.ID.ToString();
                        if (request.ServerVariables["QUERY_STRING"] != null)
                        {
                            realURL += "&" + request.ServerVariables["QUERY_STRING"];
                        }
                    }
                    else
                    {
                        realURL += "~/default.aspx?type=p&g=" + group.ID.ToString();
                        if (paths.Length > 1)
                        {
                            int parentID = 0;
                            for (int i = 1; i < paths.Length; i++)
                            {
                                string mText = paths[i].Trim();
                                if (mText.Equals("page", StringComparison.OrdinalIgnoreCase))
                                {
                                    realURL += "&" + RewriteModule.GetPageQuery(paths, i);
                                    break;
                                }
                                else if (RewriteModule.IsContentID(mText))
                                {
                                    realURL += "&id=" + RewriteModule.GetContentID(mText);
                                }
                                else
                                {
                                    //int mnuID = RewriteModule.GetMenuID(mText, group.ID, parentID);
                                    CommonMenu mnu = RewriteModule.GetMenu(mText, group.ID, parentID);
                                    string mURL = RewriteModule.GetMenuQuery(mnu.ID, i);
                                    if (mURL != string.Empty)
                                    {
                                        realURL += "&" + mURL;
                                    }
                                    parentID = mnu.ID;

                                    if (mnu.LinkType == 3)
                                    {
                                        int pParentID = 0;
                                        for (int j = i + 1; j < paths.Length; j++)
                                        {
                                            string sText = paths[j].Trim();
                                            if (sText.Equals("page", StringComparison.OrdinalIgnoreCase))
                                            {
                                                realURL += "&" + RewriteModule.GetPageQuery(paths, j);
                                                break;
                                            }
                                            else if (RewriteModule.IsContentID(sText))
                                            {
                                                realURL += "&id=" + RewriteModule.GetContentID(sText);
                                            }
                                            else
                                            {
                                                ProductType type = new ProductType(1, pParentID, sText);
                                                string sURL = RewriteModule.GetProductMenuQuery(type.ID, j - i);
                                                if (sURL != string.Empty)
                                                {
                                                    realURL += "&" + sURL;
                                                }
                                                pParentID = type.ID;
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (realURL != "")
            {
                context.RewritePath(realURL);
            }
        }

        private static string GetPageQuery(string[] paths, int index)
        {
            string pageNo = "1";
            if (paths.Length > index + 1)
            {
                pageNo = paths[index + 1].Trim();
            }
            return "page=" + pageNo;
        }

        private static int GetMenuID(string path, int gID, int parentID)
        {
            CommonMenu menu = new CommonMenu(gID, parentID, path);
            return menu.ID;
        }

        private static CommonMenu GetMenu(string path, int gID, int parentID)
        {
            CommonMenu menu = new CommonMenu(gID, parentID, path);
            return menu;
        }

        private static string GetMenuQuery(int menuID, int level)
        {
            switch (level)
            {
                case 1:
                    return "m=" + menuID.ToString();
                case 2:
                    return "s=" + menuID.ToString();
                case 3:
                    return "x=" + menuID.ToString();
                case 4:
                    return "t=" + menuID.ToString();
                case 5:
                    return "u=" + menuID.ToString();
                default:
                    return string.Empty;
            }
        }

        private static string GetProductMenuQuery(int menuID, int level)
        {
            switch (level)
            {
                case 1:
                    return "pm=" + menuID.ToString();
                case 2:
                    return "ps=" + menuID.ToString();
                case 3:
                    return "px=" + menuID.ToString();
                case 4:
                    return "pt=" + menuID.ToString();
                case 5:
                    return "pu=" + menuID.ToString();
                default:
                    return string.Empty;
            }
        }

        private static bool IsContentID(string path)
        {
            return (path.EndsWith(".html", StringComparison.OrdinalIgnoreCase)
                || path.EndsWith(".htm", StringComparison.OrdinalIgnoreCase));
        }

        private static string GetContentID(string path)
        {
            string idText = "";
            if (path.EndsWith(".html", StringComparison.OrdinalIgnoreCase))
            {
                idText = path.Remove(path.Length - ".html".Length);
            }
            else if (path.EndsWith(".htm", StringComparison.OrdinalIgnoreCase))
            {
                idText = path.Remove(path.Length - ".htm".Length);
            }
            string[] ids = idText.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
            if (ids.Length > 0)
            {
                return ids[ids.Length - 1];
            }

            return string.Empty;
        }
        /*
        private static string GetMenuQuery(string path, int level, int gID, string parentID)
        {
            if (path.EndsWith(".html", StringComparison.OrdinalIgnoreCase)
                || path.EndsWith(".htm", StringComparison.OrdinalIgnoreCase))
            {
                string idText = "";
                if (path.EndsWith(".html", StringComparison.OrdinalIgnoreCase))
                {
                    idText = path.Remove(path.Length - ".html".Length);
                }
                else if (path.EndsWith(".htm", StringComparison.OrdinalIgnoreCase))
                {
                    idText = path.Remove(path.Length - ".htm".Length);
                }
                string[] ids = idText.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
                if (ids.Length > 0)
                {
                    string id = ids[ids.Length - 1];
                    return "id=" + id;
                }
                return string.Empty;
            }
            else
            {
                CommonMenu menu = new CommonMenu(gID, Convert.ToInt32(parentID), path);
                if (menu.ID > 0)
                {
                    switch (level)
                    {
                        case 1:
                            return "m=" + menu.ID.ToString();
                        case 2:
                            return "s=" + menu.ID.ToString();
                        case 3:
                            return "x=" + menu.ID.ToString();
                        case 4:
                            return "t=" + menu.ID.ToString();
                        default:
                            return "u=" + menu.ID.ToString();
                    }
                }
                return string.Empty;
            }
        }
        */
    }
}